package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UploadFolderPeer extends BasePeer{
	public static final String tableName = "_Upload_Folder";
	public static final String UPLOADID = "uploadId";
	public static final String FOLDERID = "folderId";
	public static final String USERID = "userId";
	public static final String[] columnNames=new String[]{"uploadId","folderId","userId"};
	public static UploadFolder getUploadFolderByPKs(Object uploadId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadFolderPeer.tableName,UploadFolderPeer.columnNames," TRUE  AND `uploadId`=?  AND `userId`=? ",new Object[]{uploadId,userId,},0, 1,conn);
			UploadFolder uploadFolder = new UploadFolder();
		fill(rs,uploadFolder);
		return uploadFolder;
	}
	public static UploadFolder getUploadFolderReferenceColumnsOnlyByPKs(Object uploadId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadFolderPeer.tableName,new String[]{"uploadId","userId",}," TRUE AND `uploadId`=?AND `userId`=?",new Object[]{uploadId,userId,}, 0,  1,conn);
			UploadFolder uploadFolder = new UploadFolder();
		fill(rs,uploadFolder);
		return uploadFolder;
	}
	public static UploadFolder getUploadFolderByUploadId(long uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadFolderPeer.tableName,UploadFolderPeer.columnNames,"`uploadId`=?",new Object[]{uploadId},0, 1,conn);
			UploadFolder uploadFolder = new UploadFolder();
		fill(rs,uploadFolder);
		return uploadFolder;
	}
	public static UploadFolder getUploadFolderByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadFolderPeer.tableName,UploadFolderPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			UploadFolder uploadFolder = new UploadFolder();
		fill(rs,uploadFolder);
		return uploadFolder;
	}
	public static void fill(ResultSet rs, UploadFolder uploadFolder) throws SQLException {
		if(rs.next()){
			try{uploadFolder.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{uploadFolder.setFolderId(rs.getLong("folderId"));}catch(Exception ex){}
			try{uploadFolder.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			uploadFolder.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UploadFolder> uploadFolderCollection) throws SQLException {
		while(rs.next()){
			UploadFolder uploadFolder=new UploadFolder();
			try{uploadFolder.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{uploadFolder.setFolderId(rs.getLong("folderId"));}catch(Exception ex){}
			try{uploadFolder.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			uploadFolder.resetModified();
			uploadFolderCollection.add(uploadFolder);
		}
	}
	public static boolean update(UploadFolder uploadFolder, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UploadFolderPeer.columnNames.length; i++) {
			if (((UploadFolderBase)uploadFolder).modified[i]) {
				columns.add(UploadFolderPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_Upload_Folder` SET ");
		sql.append("WHERE TRUE AND `uploadId` = ? AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,uploadFolder.get(column));
		}
		pstmt.setObject(columns.size()+1+0,uploadFolder.get("uploadId"));
		pstmt.setObject(columns.size()+1+1,uploadFolder.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UploadFolder uploadFolder, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (uploadFolder.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_Upload_Folder` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (uploadFolder.get(column) != null) {
				pstmt.setObject(i + 1 + j, uploadFolder.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UploadFolder> getUploadFolderCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UploadFolder> collection=new Vector<UploadFolder>();
		fill(rs,collection);
		return collection;
	}
}
